package demo_0428;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonClickHandlerDemo{

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());
        frame.setBounds(100,100,400,300);
        JTextField textField = new JTextField();
        textField.setColumns(20);
        JButton button = new JButton("提交");
        //MyButtonListener listener = new MyButtonListener(textField);
       // button.addActionListener(listener);
        button.addActionListener(e -> System.out.println("按钮被点击了"));

        frame.add(textField);
        frame.add(button);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true );


    }


}

class MyButtonListener implements ActionListener {
    JTextField textField;
    public MyButtonListener(JTextField textField){
        this.textField = textField;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 在这里处理按钮点击事件，例如获取文本框中的内容并打印出来
        System.out.println("按钮被点击了");
        String text = textField.getText();
        System.out.println("文本框中的内容是：" + text);
    }
}
